package com.github.binarywang.demo.wechat.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.binarywang.demo.wechat.model.Course;
import com.github.binarywang.demo.wechat.model.ResponseResult;
import com.github.binarywang.demo.wechat.model.vo.CourseVO;
import com.github.binarywang.demo.wechat.service.WxCourseService;
import com.github.binarywang.demo.wechat.utils.FileUtils;
import com.github.binarywang.demo.wechat.utils.JsonUtils;

import cn.binarywang.wx.miniapp.api.WxMaService;

@RequestMapping({ "wechat/course" })
@RestController
public class WxCourseController {
	@Autowired
	private WxCourseService wxCourseService;
	@Autowired
	private WxMaService wxService;

	@GetMapping({ "list" })
	public String listCourse(Integer start, String openId) {
		return JsonUtils.toJson(this.wxCourseService.list(start, openId));
	}

	@GetMapping({ "findByType" })
	public String getCourseByType(String typeId, Integer start, String openId) {
		return JsonUtils.toJson(this.wxCourseService.findByTypeId(typeId, start, openId));
	}

	@GetMapping({ "findById" })
	public String getCourseById(Integer courseId, String openId) {
		return JsonUtils.toJson(this.wxCourseService.findById(courseId, openId));
	}

	@GetMapping({ "insert" })
	public int insertCourse(Course course, String imgMediaId, boolean isRefer) throws Exception {
		File imgFile = this.wxService.getMediaService().getMedia(imgMediaId);
		course.setCreatetime(new Date());
		course.setCover(FileUtils.writeImageToLocal(imgFile));
		return this.wxCourseService.insert(course, isRefer);
	}
	
	@GetMapping({ "insert_" })
	public int insertCourse_(Course course, boolean isRefer) throws Exception {
		course.setCreatetime(new Date());
		return this.wxCourseService.insert(course, isRefer);
	}

	@GetMapping({ "update" })
	public int updateCourse(Course course, String imgMediaId) throws Exception {
		File imgFile = this.wxService.getMediaService().getMedia(imgMediaId);
		course.setCreatetime(new Date());
		course.setCover(FileUtils.writeImageToLocal(imgFile));
		return this.wxCourseService.update(course);
	}

	@GetMapping({ "delete" })
	public int deleteCourse(Integer courseId, String openId) {
		return this.wxCourseService.delete(courseId, openId);
	}

	@GetMapping({ "refer" })
	public String referCourse() {
		return JsonUtils.toJson(this.wxCourseService.refer());
	}

	@GetMapping({ "search" })
	public String searchCourse(String content) {
		return JsonUtils.toJson(this.wxCourseService.search(content));
	}

	@PostMapping({ "upload" })
	public String uploadFile(MultipartFile file) throws Exception {
		String filePath = FileUtils.writeVideoToLocal(file);
		String duration = FileUtils.getVideoDuration(filePath);
		Map<String, String> result = new HashMap<String, String>();
		result.put("filePath", filePath);
		result.put("duration", duration);
		return JsonUtils.toJson(result);
	}
	
	@PostMapping({ "uploadFile" })
	public ResponseResult<String> uploadFileWeb(MultipartFile file) throws Exception {
		ResponseResult<String> result = new ResponseResult<String>();
		List<String> resultList = new ArrayList<String>();
		try {
			String filePath = FileUtils.writeFileToLocal(file);
			if (file.getContentType().contains("video")) {
				String duration = FileUtils.getVideoDuration(filePath);
				resultList.add(0, filePath);
				resultList.add(1, duration);
			} else if (file.getContentType().contains("image")) {
				resultList.add(0, filePath);
			}
			result.setCode(0);
			result.setMsg("上传成功");
			result.setData(resultList);
		} catch (Exception ex) {
			result.setCode(-1);
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@GetMapping({ "search2" })
	@ResponseBody
	public ResponseResult<CourseVO> searchCourse2(String content) {
		ResponseResult<CourseVO> result = new ResponseResult<CourseVO>();
		List<CourseVO> data = this.wxCourseService.search(content);
		result.setCode(0);
		if(data.size() > 0 ) {
			result.setMsg("查询成功");
			result.setCount(data.size());
		}else{
			result.setMsg("没有查询到数据");
			result.setCount(0);
		}
		result.setData(data);
		return result;
	}
	
	@PostMapping({ "update_" })
	public int updateCourse(Course course) throws Exception {
		course.setCreatetime(new Date());
		return this.wxCourseService.update(course);
	}
	
}
